package com.shop.loan.redis;

import com.alibaba.fastjson.JSON;
import com.shop.loan.mapper.SettingByMybatisMapper;
import com.shop.loan.model.entity.SettingByMybatis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Slf4j
@Component
public class RedisCache {

    public static final String redisKey = "shop_loan_";

    public static final String rentalUserLogin = redisKey + "rentalUserLogin_";

    public static final String userLoginPicCode = redisKey + "userLoginPicCode_";

    //用户登录失败数量
    public static final String userLoginFailNumIp = redisKey + "userLoginFailNumIp_";
    //管理员操作密码错误数量
    public static final String userOpFailNumIp = redisKey + "userOpFailNumIp_";

    public static final String blackListByIp = redisKey + "blackListByIp";


    public static final String eruptUserLastLoginIp = redisKey + "eruptUserLastLoginIp_";

    public static final String phoneCode = redisKey + "phoneCode_";

    //小程序首页接口
    public static final String appHomeData = redisKey + "appHomeData";

    //是否在更新物流
    public static final String updateLogisticsByIngLock = redisKey + "updateLogisticsByIng_";

    //手机验证码发送次数
    public static final String phoneCodeNum = redisKey + "phoneCodeNum_";

    //订单上传状态
    public static final String orderPutStatus = redisKey + "orderPutStatus_";
    //订单紧急联系人上传状态
    public static final String overdueOrderContactPutStatus = redisKey + "overdueOrderContactPutStatus_";
    //拒绝用户紧急联系人上传状态
    public static final String rejectUserContactPutStatus = redisKey + "rejectUserContactPutStatus_";

    public static final String putBlackLastId = redisKey + "putBlackLastId";

    public static final String putBlackLastIdByUser = redisKey + "putBlackLastIdByUser";

    public static final String putRejectUserId = redisKey + "putRejectUserId";

    public static final String waitCreateContract = redisKey + "waitCreateContract";

    public static final String overdueOrderMsg = redisKey + "overdueOrderMsg_";

    //创建支付订单锁
    public static final String createPayLock = redisKey + "createPayLock_";

    //创建订单锁
    public static final String createLoanOrderLock = redisKey + "createLoanOrderLock_";

    public static final String alipayFormData = redisKey + "alipayFormData_";

    public static final String orderPaymentLock = redisKey + "orderPaymentLock_";

    public static final String orderAuthLock = redisKey + "orderAuthLock_";

    public static final String sysAdminMsg = redisKey + "sysAdminMsg_";

    public static final String reportLock = redisKey + "reportLock_";

    public static final String service_allot_index = redisKey + "service_allot_index_";

    //逾期代扣锁
    public static final String overdueWithholdLock = redisKey + "overdueWithholdLock";
    //今日代扣锁
    public static final String todayWithholdLock = redisKey + "todayWithholdLock";
    //发起还款锁
    public static final String payLock = redisKey + "payLock_";

    //系统设置
    public static final String settingCache = redisKey + "settingCache_";
    //手动代扣锁
    public static final String withholdAdminLock = redisKey + "withholdAdminLock_";
    //放款锁
    public static final String orderLoanLock = redisKey + "orderLoanLock_";
    //正在放款中锁
    public static final String orderLoanIngMap = redisKey + "orderLoanIngMap";

    public static final String orderLoanSuccessMap = redisKey + "orderLoanSuccessMap";


    public static final String channelPageOpenUv = redisKey + "channelPageOpenUv";

    public static final String channelPageOpenPv = redisKey + "channelPageOpenPv";

    public static final String registerMobileMap = redisKey + "registerMobileMap";

    public static final String company = redisKey + "company_";

    //代扣成功（本息）
    public static final String withholdSuccessByPi = redisKey + "withholdSuccessByPi_";
    //代扣成功（担保费）
    public static final String withholdSuccessByGu = redisKey + "withholdSuccessByGu_";

    public static final String arContract = redisKey + "arContract";

    public static final String arNotifyLock = redisKey + "arNotifyLock";

    public static final String arNotifyQueue = redisKey + "arNotifyQueue";
    //推送结果失败次数
    public static final String arNotifyFailNum = redisKey + "arNotifyFailNum";

    public static final String yingCaiApplyIdQueue = redisKey + "yingCaiApplyIdQueue";
    public static final String yingCaiApplyIdGetLock = redisKey + "yingCaiApplyIdGetLock";


    @Resource
    SettingByMybatisMapper settingByMybatisMapper;

    @Resource
    RedisUtil redisUtil;

    static {


    }


    /**
     * 获取系统设置
     *
     * @param fromDb 从数据库重新获取
     * @return
     */
    public SettingByMybatis getSetting(Boolean fromDb) {
        long settingId = 1L;
        String dbKeyInfo = redisUtil.get(settingCache);
        if (StringUtils.hasLength(dbKeyInfo) && !fromDb) {
            return JSON.parseObject(dbKeyInfo, SettingByMybatis.class);
        }
        SettingByMybatis settingByMybatis = settingByMybatisMapper.selectByPrimaryKey(settingId);
        if (settingByMybatis == null || settingByMybatis.getId() == null) {
            return null;
        }
        redisUtil.set(settingCache, JSON.toJSONString(settingByMybatis));
        return settingByMybatis;
    }


    /**
     * 校验手机号码是否已注册
     *
     * @param mobile
     * @return
     */
    public Boolean checkMobileIsRegister(String mobile) {
        if (redisUtil.hget(registerMobileMap, mobile) != null) {
            return true;
        }
        return false;
    }

}
